package com.cmz.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * @author summer
 * @create 2020-02-10 0:11
 */
public class TankFrame extends Frame {

    Tank myTank = new Tank(200,400,Dir.DOWN,Group.GOOD,this);

    // 实例化子弹
    List<Bullet> bullets = new ArrayList<>();

    // 实例化地方tank
    List<Tank> tanks = new ArrayList<>();

    //爆炸
    List<Explode> explodes = new ArrayList<>();


//    Bullet b = new Bullet(300,300,Dir.DOWN,,this);

    static final int GAME_WIDITH = 1080; // 坦克画布长度
    static final int GAME_HEIGHT = 960;  // 坦克画布宽度

    // tank类构造函数
    public TankFrame() throws HeadlessException {
        setSize(GAME_WIDITH,GAME_HEIGHT); // 设置游戏界面大小
        setResizable(false);              // 设置是否能改变大小
        setTitle("tank game");            // 设置标题
        setVisible(true);                 // 显示游戏界面窗体

        // 添加指定的按键侦听器，以接收发自此组件的按键事件。如果 l 为 null，则不会抛出异常并且不执行动作。
        this.addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    // 双缓冲 解决闪烁问题，内存内容[画好的]，一次性放入缓存，解决闪烁问题
    Image offScreenImage = null;
    @Override
    public void update(Graphics g){
        if(offScreenImage == null){
            offScreenImage = this.createImage(GAME_WIDITH,GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0,GAME_WIDITH,GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage,0,0,null);
    }

    @Override
    public void paint(Graphics g){ // 画笔g 每当窗口改变就会自动 调用
        Color c = g.getColor();
        g.setColor(Color.white);
        g.drawString("子弹的数量: "+bullets.size(),10,60);
        g.drawString("敌人的数量: "+tanks.size(),10,80);
        g.drawString("爆炸的数量: "+explodes.size(),10,100);
        g.setColor(c);
        myTank.paint(g);


        // 画子弹
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
        //画坦克
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        // 画爆炸
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }

        // 子弹碰撞坦克检查
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }



//        for (Iterator<Bullet> it = bullets.iterator();it.hasNext();) {
//            Bullet b = it.next();
//            if(!b.live) it.remove();
//        }
    }

    // 键盘监听事件
    class MyKeyListener extends KeyAdapter{

        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            // 按键被按下就为true
            switch(key){
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                case KeyEvent.VK_CONTROL:
                    myTank.fire();
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            // 按键被抬起
            switch(key){
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {
            if(!bL && !bU && !bR && !bD) myTank.setMoving(false);
            else{
                myTank.setMoving(true);
                if(bL) myTank.setDir(Dir.LEFT);
                if(bU) myTank.setDir(Dir.UP);
                if(bR) myTank.setDir(Dir.RIGHT);
                if(bD) myTank.setDir(Dir.DOWN);
            }
        }
    }
}
